using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

namespace Tools.UI.InputEvent
{
    [RequireComponent(typeof(RectTransform))][RequireComponent(typeof(RectTransform))][AddComponentMenu("Tools/UI/OnUIScroll")]
    public class OnUIScroll : MonoBehaviour, IPointerDownHandler,IPointerUpHandler,IDragHandler
    {
        [SerializeField][Header("为空时 自动获取父节点")]
        RectTransform anchorRectTransform;
        
        RectTransform _rectTransform;
        public RectTransform URectTransform
        {
            get
            {
                if(_rectTransform == null)
                {
                    _rectTransform = GetComponent<RectTransform>();
                }
                return _rectTransform;
            }
        }
        
        public  RectTransform UParentRectTransform
        {
            get
            {
                if(anchorRectTransform == null)
                {
                    anchorRectTransform = URectTransform.parent.GetComponent<RectTransform>();
                }
                return anchorRectTransform;
            }
        }
        
        [SerializeField]
        UnityEvent<OnUIScroll,PointerEventData> onReady = new UnityEvent<OnUIScroll,PointerEventData>();
        public UnityEvent<OnUIScroll,PointerEventData> OnDragReady => onReady;
        
        [SerializeField]
        UnityEvent<OnUIScroll,PointerEventData> onEnd = new UnityEvent<OnUIScroll,PointerEventData>();
        public UnityEvent<OnUIScroll,PointerEventData> OnDragEnd => onEnd;
        
        [SerializeField]
        UnityEvent<OnUIScroll,PointerEventData>onMove = new UnityEvent<OnUIScroll,PointerEventData>();
        public UnityEvent<OnUIScroll,PointerEventData> OnMove => onMove;

        private void Start()
        {
            
        }

        void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
        {
            if (!enabled) return;
            OnDragReady?.Invoke(this,eventData);
        }

        void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
        {
            if (!enabled) return;
            OnDragEnd?.Invoke(this,eventData);
        }

        void IDragHandler.OnDrag(PointerEventData eventData)
        {
            if (!enabled) return;
            OnMove?.Invoke(this,eventData);
        }
    }
}
